package kosta.controller;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import kosta.model.board.BoardVO;
import kosta.model.board.ListVO;
import kosta.model.board.NewsBoardService;
import kosta.model.member.MemberVO;
import kosta.model.reply.ReplyService;
import kosta.model.reply.ReplyServiceImpl;
import kosta.model.reply.ReplyVO;
import kosta.model.reply.news.NewsReplyService;
import kosta.model.reply.news.NewsReplyVO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

@Controller
public class NewsBoardController extends MultiActionController {
	@Resource(name="newsBoardServiceImpl")
	private NewsBoardService	 newsBoardService;
	@Resource(name="newsReplyServiceImpl")
	private NewsReplyService replyService;

	
	@RequestMapping("/board_newsWriteView.do")
	public String newsWriteView(){
		System.out.println("controller write view");
		return "news_write.newsBoard";
	}
	@RequestMapping("/board_newswrite.do")
	public ModelAndView newsWrite(BoardVO vo,HttpSession session){
		System.out.println("controller write"+vo);
		MemberVO mvo=(MemberVO) session.getAttribute("mvo");
		vo.setMemberVO(mvo);
		System.out.println("writectrl "+vo);
		newsBoardService.newsWrite(vo);
		int no=vo.getNo();
		return new ModelAndView("redirect:board_showContentNoHit.do?no="+no);
	}
	@RequestMapping("/board_newsUpdateView.do")
	public ModelAndView newsUpdateView(int no) throws SQLException{
		System.out.println("controller update view");
		BoardVO bvo=newsBoardService.showContentNoHit(no);
		return new ModelAndView("news_update.newsBoard","bvo",bvo);
	}
	@RequestMapping("/board_newsupdate.do")
	public ModelAndView newsUpdate(BoardVO vo){
		System.out.println("controller update!"+vo);
		newsBoardService.newsUpdate(vo);
		return new ModelAndView("redirect:board_showContentNoHit.do?no="+vo.getNo());
	}
	@RequestMapping("/board_newsDelete.do")
	public ModelAndView newsDelete(int no) throws SQLException{
		System.out.println("controller delete!"+no);
		newsBoardService.newsDelete(no);
		return new ModelAndView("news_list.newsBoard","lvo",newsBoardService.getListBoard("1"));
	}
	@RequestMapping("/board_showContentNoHit.do")
	public ModelAndView showContentNoHit(int no) throws SQLException{
		BoardVO bvo = newsBoardService.showContentNoHit(no);
		List<NewsReplyVO> list = replyService.getListReply(no);
		System.out.println("re list+"+list);
		HashMap map = new HashMap();
		map.put("bvo", bvo);
		map.put("list", list);
		return new ModelAndView("news_view.newsBoard","map",map);
	}
	@RequestMapping("/board_newsSearchTitle.do")
	public ModelAndView newsSearchTitle(String text, String pageNo){
		System.out.println("title search ctrl "+text);
		System.out.println(newsBoardService.newsSearchTitle(text,pageNo));
		return new ModelAndView("news_list.newsBoard","lvo",newsBoardService.newsSearchTitle(text,pageNo));
	}
	@RequestMapping("/board_newslist.do")
	 public ModelAndView getListBoard(String pageNo,HttpServletRequest request) throws SQLException{
		ModelAndView mv=new ModelAndView("news_list.newsBoard");
		String text=request.getParameter("text");
		ListVO lvo=null;

			lvo=newsBoardService.newsSearchTitle(text,pageNo);
			mv.addObject("text",text);
		
		mv.addObject("lvo",lvo);
		System.out.println("newslist controller");
		return mv;
	}
	
	@RequestMapping("/board_newsview.do")
	public ModelAndView getViewBoard(int no) throws SQLException{
		/*System.out.println("news view controller no"+no);
		BoardVO bvo = newsBoardService.getViewBoard(no);
		List<ReplyVO> list = replyService.getListReply(no);
		HashMap map = new HashMap();
		map.put("bvo", bvo);
		map.put("list", list);*/
		newsBoardService.getViewBoard(no);
		return new ModelAndView("redirect:board_showContentNoHit.do?no="+no);
	}
	@RequestMapping("/reply_newsWirte.do")
	public ModelAndView insertReply(NewsReplyVO rvo) throws SQLException{
		System.out.println("리플레이컨트롤러 인서트"+rvo);
		replyService.insertReply(rvo);
		System.out.println("reply ctrl"+rvo.getBoardVO().getNo());
		return new ModelAndView("redirect:board_newsview.do?&no="+rvo.getBoardVO().getNo());
	}
	@RequestMapping("/news_deleteReply.do")
	public ModelAndView deleteReply(int rno, int refno,String id) throws SQLException{
		System.out.println("******deleteReply********"+rno+" "+refno);
		replyService.deleteReply(rno,id);
		return new ModelAndView("redirect:board_newsview.do?no="+refno);
	}
	
	
	@RequestMapping("/news_updateReply.do")
	public ModelAndView updateReply(NewsReplyVO rvo) throws SQLException{
		System.out.println("ctrl update"+rvo);
		replyService.updateReply(rvo);
		return new ModelAndView("redirect:board_newsview.do?no="+rvo.getBoardVO().getNo());
	}
	
	//대댓글
	@RequestMapping("/news_reReply.do")
	public ModelAndView insertReReply(NewsReplyVO rvo) throws SQLException{
		System.out.println("insertReReply"+rvo);
		replyService.insertReReply(rvo);
		return new ModelAndView("redirect:board_newsview.do?no="+rvo.getBoardVO().getNo());
	}

}